package com.mgvlife.ddlw.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.mgvlife.ddlw.entity.Schedule;
import com.mgvlife.ddlw.entity.SignInInfo;
import com.mgvlife.ddlw.formbean.SignInInfoForm;
import com.mgvlife.ddlw.service.SignInInfoService;
import com.mgvlife.ddlw.vo.Result;
import com.mgvlife.ddlw.vo.VoSignInInfo;
import com.xinhuo.dao.DaoSupport;
import com.xinhuo.exception.DaoException;
import com.xinhuo.util.DateUtil;
import com.xinhuo.util.QueryResult;
import com.xinhuo.vo.JsonEasyUI;

@Service("signInInfoService")
public class SignInInfoServiceimpl extends DaoSupport implements
		SignInInfoService {

	public Result signInInfoUpdate(SignInInfoForm dataForm) throws Exception {
		String idstr = dataForm.getId();
		if (idstr != null && idstr.length() > 0) {
			String whereSQL = "";
			String[] ids = idstr.split(";");
			boolean isFirst = true;
			for (int i = 0; i < ids.length; i++) {
				if (isFirst) {
					whereSQL = "id=?";
					isFirst = false;
				} else {
					whereSQL += " or id=?";
				}
			}
			List<SignInInfo> list = this.findAll(SignInInfo.class, whereSQL,
					ids);
			for (SignInInfo signInInfo : list) {
				switch (Integer.parseInt(dataForm.getStatus())) {
				case -1:
					signInInfo.setSignInStatus(-1);
					signInInfo.setSignInTime(null);
					this.update(signInInfo);
					break;
				case 1: 
					signInInfo.setSignInStatus(1);
					signInInfo.setSignInTime(DateUtil.dataToString(new Date()));
					this.update(signInInfo);
					break;
				case 0:
					signInInfo.setSignInStatus(0);
					signInInfo.setSignInTime(DateUtil.dataToString(new Date()));
					this.update(signInInfo);
					break;
				case 2:
					signInInfo.setSignInStatus(2);
					signInInfo.setSignInTime(DateUtil.dataToString(new Date()));
					break;
				case 3:
					signInInfo.setSignInStatus(3);
					signInInfo.setSignInTime(null);
					break;
				case 4:
					signInInfo.setSignInStatus(4);
					signInInfo.setSignInTime(null);
					break;
				default:
					break;
				}
			}
		} else {
			return new Result("error", "该用户不存在");
		}
		return Result.success();
	}

	public JsonEasyUI<VoSignInInfo> getData(SignInInfoForm dataForm)
			throws DaoException {
		JsonEasyUI<VoSignInInfo> result = new JsonEasyUI<VoSignInInfo>();
		Schedule schedule = this.findById(Schedule.class,
				dataForm.getScheduleId());
		String whereSQL = "schedule=?";
		Object[] whereParams = { schedule };
		QueryResult<SignInInfo> qr = this.getScrollData(SignInInfo.class,
				whereSQL, whereParams, dataForm.getSearchTitle(),
				dataForm.getSearch(), dataForm.getPage(), dataForm.getRows(),
				dataForm.getSort(), dataForm.getOrder());
		result.setTotal(qr.getTotalrecord());
		List<VoSignInInfo> list = new ArrayList<VoSignInInfo>();
		for (SignInInfo signInInfo : qr.getResultList()) {
			list.add(new VoSignInInfo(signInInfo));
		}
		result.setRows(list);
		return result;
	}

	public JsonEasyUI<VoSignInInfo> getInfos(SignInInfoForm dataForm)
			throws Exception {
		JsonEasyUI<VoSignInInfo> result = new JsonEasyUI<VoSignInInfo>();
		Schedule schedule = this.findById(Schedule.class,
				dataForm.getScheduleId());
		if (schedule == null) {
			return null;
		}
		String whereSQL = "schedule=? and signInStatus=1";
		Object[] whereParams = { schedule };
		List<SignInInfo> qr = this.findAll(SignInInfo.class, whereSQL,
				whereParams);
		result.setTotal((long) qr.size());
		List<VoSignInInfo> list = new ArrayList<VoSignInInfo>();
		for (SignInInfo signInInfo : qr) {
			list.add(new VoSignInInfo(signInInfo));
		}
		result.setRows(list);
		return result;
	}

}
